﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace StockLib.Quote
{
    public partial class StockReceiver
    {
        /// <summary>
        /// 补充分时数据
        /// </summary>
        /// <param name="tsdata">通视数据</param>
        private void ReceiveFileMinute(ref ReceiveData tsdata)
        {
            Dictionary<Stock, List<KData>> kqueue = new Dictionary<Stock, List<KData>>();
            List<KData> kdata = null;
            Stock stock = null;
            ReceiveMinuteFilePack pack;
            ReceiveFileHead head;

            try
            {
                int packSize = Marshal.SizeOf(typeof(ReceiveMinuteFilePack));
                for (int i = 0; i < tsdata.m_nPacketNum; i++)
                {
                    IntPtr pointer = new IntPtr((int)tsdata.m_pData + i * packSize);
                    head = (ReceiveFileHead)Marshal.PtrToStructure(pointer, typeof(ReceiveFileHead));
                    pack = (ReceiveMinuteFilePack)Marshal.PtrToStructure(pointer, typeof(ReceiveMinuteFilePack));
                    if (head.m_dwHeadTag == EKE_HEAD_TAG)
                    {
                        if (kdata != null && stock != null && kdata.Count > 0)
                        {
                            kqueue.Add(stock, kdata);
                        }

                        // 保存股票信息
                        stock = new Stock();
                        stock.Market = ConvertMarketToInternal(head.m_wMarket);
                        stock.Code = head.m_szLabel;

                        kdata = new List<KData>();
                    }
                    else
                    {
                        // 转换数据包
                        KData k = new KData();
                        ConvertMinuteFilePackToInternal(ref pack, k);
                        kdata.Add(k);
                    }
                }
                if (kdata != null && stock != null && kdata.Count > 0)
                {
                    kqueue.Add(stock, kdata);
                }

                //保存到文件
                SaveKData(kqueue, KType.Min);

                // 通知分时数据接收
                OnDataChanged(this, new ReceiverDataEventArgs(DataType.Minute));
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("文件操作出错 股票代码:{0} 异常信息:{1}", stock.Code, ex.ToString()));
                throw ex;
            }
            finally
            {
                kqueue = null;
            }
        }

        private void ConvertMinuteFilePackToInternal(ref ReceiveMinuteFilePack minutePack, KData minuteData)
        {
            minuteData.Time = minutePack.m_time;
            minuteData.High = minutePack.m_fPrice;
            minuteData.Low = minutePack.m_fPrice;
            minuteData.Open = minutePack.m_fPrice;
            minuteData.Close = minutePack.m_fPrice;
            minuteData.Volume = minutePack.m_fVolume;
            minuteData.Amount = minutePack.m_fAmount;
        }
    }
}
